package com.voute.View.Fragement;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.Base.BaseFragment;
import com.voute.Model.Bean.FeedCirclePushMessageEntity;
import com.voute.Model.Bean.PersonInfoBean;
import com.voute.Model.Bean.SimplePushMessageBean;
import com.voute.Presenter.IPersonInfoPresenter;
import com.voute.Presenter.Impl.PersonInfoPresenterImpl;
import com.voute.R;
import com.voute.Utils.GlideUtils;
import com.voute.Utils.PreferenceUtils;
import com.voute.View.Activity.CirclesListActivity;
import com.voute.View.Activity.ModifyPersonInfoActivity;
import com.voute.View.Activity.PersonalInfoFeedsActivity;
import com.voute.View.Activity.SettingActivity;
import com.voute.View.Activity.SimplePushMessageActivity;
import com.voute.View.Activity.WebViewActivity;
import com.voute.View.IView.IPersonInfoView;

import de.greenrobot.event.EventBus;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人中心Fragment
 * Created by liucxu on 2017/1/20.
 */

public class PersonalInfoFragment extends BaseFragment implements IPersonInfoView {

    private CircleImageView profile_image;
    private TextView influnce_num_tv;
    private TextView partici_num_tv;
    private TextView approve_tv;
    private TextView user_name_tv;
    private TextView personal_signature;
    private RelativeLayout personal_create_layout;
    private RelativeLayout personal_star_layout;
    private RelativeLayout personal_participate_layout;
    private RelativeLayout top_image_layout;
    private ImageView bg_imageview;
    private ImageView person_info_setting;
    private ImageView person_info_message;
    private IPersonInfoPresenter presenter;
    private MaterialDialog progressDialog;
    private RelativeLayout personal_circle_layout;
    private TextView tv_my_circle_notify;
    private RelativeLayout personal_notify_layout;
    private TextView tv_my_notify_notify;
    private RelativeLayout personal_contact_layout;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_person_info;
    }

    @Override
    public void setupView() {
        profile_image = $(R.id.profile_image);
        approve_tv = $(R.id.approve_tv);
        partici_num_tv = $(R.id.partici_num_tv);
        influnce_num_tv = $(R.id.influnce_num_tv);
        user_name_tv = $(R.id.user_name_tv);
        personal_signature = $(R.id.personal_signature);
        personal_create_layout = $(R.id.personal_create_layout);
        personal_star_layout = $(R.id.personal_star_layout);
        personal_participate_layout = $(R.id.personal_participate_layout);
        top_image_layout = $(R.id.top_image_layout);
        bg_imageview = $(R.id.bg_imageview);
        personal_contact_layout=$(R.id.personal_contact_layout);
        personal_contact_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("webview_title","联系我们");
                intent.putExtra("webview_url","https://5miaoapp.com/contact");
                getActivity().startActivity(intent);
            }
        });
        person_info_setting = $(R.id.person_info_setting);
        person_info_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), SettingActivity.class);
                getActivity().startActivity(intent);
            }
        });
        person_info_message = $(R.id.person_info_message);
        person_info_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        presenter = new PersonInfoPresenterImpl(this);
        profile_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), ModifyPersonInfoActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    @Override
    public void setupData() {
//        presenter.getMyInfo();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(PreferenceUtils.GetUserToken())) {
            presenter.getMyInfo();
            presenter.refreshPushMsg();
        }
    }

    @Override
    public void showPersonInfo(PersonInfoBean info) {
        if (TextUtils.isEmpty(info.getData().getUser().getAvatar())) {
            profile_image.setImageResource(R.mipmap.default_avaster);
//            bg_imageview.setImageResource(R.mipmap.default_avaster);
        } else {
            GlideUtils.getInstance().loadSimpleContextVitMap(getActivity(), info.getData().getUser().getAvatar(), profile_image);
//            GlideUtils.getInstance().loadSimpleContextVitMap(getActivity(),info.getData().getAvatar(),bg_imageview);
        }
        user_name_tv.setText(info.getData().getUser().getUsername());
        if (TextUtils.isEmpty(info.getData().getUser().getDescription())) {
            personal_signature.setText("这个人很懒什么都没留下");
        } else {
            personal_signature.setText(info.getData().getUser().getDescription());
        }
        personal_create_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PersonalInfoFeedsActivity.class);
                intent.putExtra("FEED_TYPE", "created");
                getActivity().startActivity(intent);
            }
        });
        personal_star_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PersonalInfoFeedsActivity.class);
                intent.putExtra("FEED_TYPE", "collected");
                getActivity().startActivity(intent);
            }
        });
        personal_participate_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), PersonalInfoFeedsActivity.class);
                intent.putExtra("FEED_TYPE", "joined");
                getActivity().startActivity(intent);
            }
        });
        personal_circle_layout = $(R.id.personal_circle_layout);
        personal_circle_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), CirclesListActivity.class);
                getActivity().startActivity(intent);
            }
        });
        tv_my_circle_notify = $(R.id.tv_my_circle_notify);
        personal_notify_layout = $(R.id.personal_notify_layout);
        personal_notify_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SimplePushMessageActivity.class);
                getActivity().startActivity(intent);
            }
        });
        tv_my_notify_notify = $(R.id.tv_my_notify_notify);
    }

    @Override
    public void refreshPushMessages(SimplePushMessageBean msg) {
        if (msg.getStatus() == 0) {
            if (msg.getData().isCircle()) {
                tv_my_circle_notify.setVisibility(View.VISIBLE);
            } else {
                tv_my_circle_notify.setVisibility(View.INVISIBLE);
            }

            if (msg.getData().isFeed()) {
                tv_my_notify_notify.setVisibility(View.VISIBLE);
            } else {
                tv_my_notify_notify.setVisibility(View.INVISIBLE);
            }

            if (msg.getData().isCircle() || msg.getData().isFeed()) {
                FeedCirclePushMessageEntity event = new FeedCirclePushMessageEntity();
                if (msg.getData().isCircle()) {
                    event.setCircle(true);
                } else {
                    event.setCircle(false);
                }
                if (msg.getData().isFeed()) {
                    event.setFeed(true);
                } else {
                    event.setFeed(false);
                }
                EventBus.getDefault().post(event);
            }
        } else {
            showError(msg.getMsg());
        }
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(getActivity())
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
//        progressDialog.show();
    }

    @Override
    public void hideLoading() {
//        progressDialog.dismiss();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if(!hidden){
            presenter.refreshPushMsg();
        }
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(profile_image,errMsg,Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(getActivity()).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    public void onEvent(FeedCirclePushMessageEntity msg) {
        if (msg.isCircle()) {
            tv_my_circle_notify.setVisibility(View.VISIBLE);
        } else {
            tv_my_circle_notify.setVisibility(View.INVISIBLE);
        }

        if (msg.isFeed()) {
            tv_my_notify_notify.setVisibility(View.VISIBLE);
        } else {
            tv_my_notify_notify.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
}
